var ArticleModel = require('./model/article.js');
var ArticlecateModel = require('./model/articlecate.js');
var User = require('./model/user');
//查询Article 表的数据

// ArticleCateAdd();
// userAdd();
// ArticleAdd()
queryAllArticle(); //查询全部新闻信息
function userAdd() {
  let user = new User({
    username: "li", //账号
    password: "123456789", //密码
    name: "李四", //姓名
    age: 26,
    sex: "男",
    tel: 129848332123,
  });
  user.save()
}

function ArticleCateAdd() {
  let articlecate = new ArticlecateModel({
    title: "国际新闻",
    descripton: "国际新闻描述",
  })
  articlecate.save()
}

function ArticleAdd() {
  let article = new ArticleModel({
    title: "香港文汇报：香港的经济很稳定",
    cid: "62148858bee2c09d4a76152b",
    /*分类 id*/
    author_id: "621489155824ef793d7c476c",
    /*用户的id*/
    author_name: "张三",
    descripton: "香港很好",
    content: "香港的经济很稳定，一片大好"
  })

  article.save()
}

function queryAllArticle() {
  //三个表关联查询
  ArticleModel.aggregate([{
      $lookup: {
        from: "articlecate",
        localField: "cid",
        foreignField: "_id",
        as: "cate"
      }
    },
    {
      $lookup: {
        from: "user",
        localField: "author_id",
        foreignField: "_id",
        as: "user"
      }
    }
  ], function (err, docs) {
    console.log(JSON.stringify(docs));
  })
}